<!--
 * @Author: early-autumn
 * @Date: 2020-05-20 17:56:34
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-06-17 15:38:25
-->
<template>
  <div>
    <el-form ref="form" :model="form" size="small" :rules="rules" inline>
      <el-form-item prop="keyword">
        <el-input v-model="form.keyword" placeholder="姓名/手机号">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="handleSearchPatientVisible"
            >搜索</el-button
          >
        </el-input>
      </el-form-item>
      <!-- <el-form-item>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleCreateTemporaryPatientVisible"
        >
          新建临时患者
        </el-button>
      </el-form-item> -->
    </el-form>

    <el-form v-if="patient" inline>
      <el-row>
        <el-col :span="6">
          <el-form-item label="姓名：">{{ patient.name }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="年龄："> {{ patient.age }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别：">{{
            formatterDict(patient.sex, 'sexOptions')
          }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号：">
            {{ patient.phoneNumber }}</el-form-item
          >
        </el-col>
      </el-row>
    </el-form>

    <search-patient
      v-model="searchPatientVisible"
      :keyword="form.keyword"
      @ok="handlePatient"
    />

    <!-- <create-temporary-patient
      v-model="createTemporaryPatientVisible"
      @ok="handlePatient"
    /> -->
  </div>
</template>

<script>
import { mapGetters, mapState } from 'vuex'
import SearchPatient from './SearchPatient'
import { listPerson, addPerson } from '@/api/services/person'

// import CreateTemporaryPatient from './CreateTemporaryPatient'

export default {
  name: 'UserMatcher',
  components: {
    SearchPatient,
    // CreateTemporaryPatient,
  },
  data() {
    return {
      form: {
        keyword: undefined,
      },
      rules: {
        keyword: {
          required: true,
          message: '姓名/手机号不能为空',
          trigger: 'blur',
        },
      },
      // 显示搜索患者
      searchPatientVisible: false,
      // 显示新建临时患者
      // createTemporaryPatientVisible: false,
      // 当前患者
      patient: undefined,
      market: false,
    }
  },
  computed: {
    ...mapGetters('dict', ['sexOptions']),
    ...mapState({
      clinicName: (state) => state.clinic.clinicName,
    }),
  },
  methods: {
    handleSearchPatientVisible() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.searchPatientVisible = true
        }
      })
    },

    // handleCreateTemporaryPatientVisible() {
    //   this.createTemporaryPatientVisible = true
    // },
    async handlePatient(e) {
      const { rows } = await listPerson({ page: 1 })
      this.patient = e
      const items = rows.find((item) => item.marketReservationId == e.id)
      console.log(items)
      if (items == undefined) {
        //市场用户不存在用户表里
        console.log(1)
        const from = {
          name: e.name,
          phoneNumber: e.phoneNumber,
          phoneOwnership: e.phoneOwnership,
          sex: e.sex,
          age: e.age,
          isTemporary: 0,
          marketReservationId: e.id,
          arriveClinic: this.clinicName,
        }
        const { code, data } = await addPerson(from)
        if (code === 200) {
          this.$emit('change', {
            name: e.name,
            phoneNumber: e.phoneNumber,
            sex: e.sex,
            age: e.age,
            personStatus: '新客首次',
            appointmentStatus: 1,
            id: data,
          })
        }
      } else {
        if (items.isTemporary == 0) {
          items.personStatus = '新客首次'
          items.appointmentStatus = 1
        } else {
          if (items.billingState == '已开单') {
            items.personStatus = '老客'
          } else {
            items.personStatus = '新客多次'
          }
          items.appointmentStatus = 1
        }
        this.$emit('change', items)
      }
    },
  },
}
</script>

<style lang="scss" scoped></style>
